package cn.zhizihua.query;

import cn.zhizihua.pojo.Menu;
import cn.zhizihua.pojo.Permission;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class PermissionQuery extends BaseQuery{

    private Long roleId; //查询条件

    private String name;
    private String url;
    private String sn;

    /*
     permission表联查role_permission表 根据role_id查询对应的权限
     select p.* from permission p left join role_permission rp on p.id=rp.permission_id where rp.role_id=?
     */
    public Wrapper getJoinWrapper() {

        MPJLambdaWrapper<Menu> wrapper = new MPJLambdaWrapper<>();
        wrapper.setAlias("p")
                .selectAll(Permission.class,"p")
                .leftJoin(" role_permission rp on p.id=rp.permission_id ");
        if (roleId!=null && roleId!=0){
            wrapper.eq("rp.role_id",roleId);
        }

        return wrapper;
    }

    /**
     * 表格分页查询数据
     * @return
     */
    @Override
    public Wrapper getQueryWrapper() {
        UpdateWrapper<Permission> wrapper = new UpdateWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            wrapper.like("name",name);
        }
        if (StringUtils.isNotEmpty(url)){
            wrapper.like("url",url);
        }
        if (StringUtils.isNotEmpty(sn)){
            wrapper.like("sn",sn);
        }
        wrapper.orderByDesc("id");
        return wrapper;
    }
}
